package top.csy8888.deadqueue.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.csy8888.deadqueue.consts.RabbitConstants;

/**
 * @author CSY
 * @date 2025 - 10 - 16
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue queue() {
        return QueueBuilder
                .nonDurable(RabbitConstants.QUEUE_NAME)
                .withArgument("x-dead-letter-exchange",RabbitConstants.DEAD_EXCHANGE_NAME)
                .withArgument("x-dead-letter-routing-key",RabbitConstants.DEAD_ROUTING_KEY)
                .build();
    }

    @Bean
    public Exchange exchange() {
        return new DirectExchange(RabbitConstants.EXCHANGE_NAME,false,true);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder
                .bind(queue())
                .to(exchange())
                .with(RabbitConstants.ROUTING_KEY)
                .noargs();
    }

    /**
     * 声明死信交换机（DLX），死信交换机也可以是Direct或者其他类型
     */
    @Bean
    public Exchange deadExchange() {
        return new DirectExchange(RabbitConstants.DEAD_EXCHANGE_NAME,false,true);
    }

    /**
     * 死信队列
     */
    @Bean
    public Queue deadQueue() {
        return new Queue(RabbitConstants.DEAD_QUEUE_NAME,false);
    }

    @Bean
    public Binding bindingDead() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(RabbitConstants.DEAD_ROUTING_KEY).noargs();
    }
}
